//
//  MoLocalizable.swift
//  momall_Example
//
//  Created by zhu xietong on 2020/4/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import Foundation
import Eelay

public class MoLocalizable{
    public static var _bundle:Bundle? = nil
    public static var bundle:Bundle?{
        get{
            guard let b = _bundle else {
                if let path = Bundle.main.path(forResource: MoLocalizable.lproj, ofType: "lproj"){
                    if let bundle = Bundle(path: path){
                        _bundle = bundle
                        return bundle
                    }
                }
                return nil
            }
            return b
        }
    }
    
    public static var isAuto = false
    
    public static var _lproj:String? = nil

    public static var lproj:String{
        get{
            
            if let str = _lproj{
//                print("+++OOSOS-----\(_lproj)")
                return str
            }
            
            let loc = Bundle.main.preferredLocalizations.first ?? "en"
            if isAuto{
                _lproj = loc
                return loc
            }
            else{
                if let v = UserDefaults.standard.value(forKey: "MoLocalizable/lproj") as? String{
                    _lproj = v
                    return v
                }
                _lproj = loc
                return loc
            }
        }
        set{
            _bundle = nil
            _lproj = newValue
            
            UserDefaults.standard.set(newValue, forKey: "MoLocalizable/lproj")
            __post(notify: TP.Notice.localizableDidChange)
        }
    }
    
    public static func localizedString(key:String,value:String?,`class`:AnyClass?,bundleName:String?,tableName:String?) -> String {
        
        var result = "\(key)"
        guard let _class = `class`, let  _bundleName = bundleName  else {
            result = NSLocalizedString("\(key)", comment: "").removeMarked
            return result
        }
                

        if let p = Bundle.path(for: _class, bundleName: _bundleName, fileName: MoLocalizable.lproj, type: "lproj"){
            if let lanBundle = Bundle(path: p){
                return lanBundle.localizedString(forKey: key, value: value, table: tableName).removeMarked
            }
        }
    
        result = NSLocalizedString("\(key)", comment: "").removeMarked
       
        return result
    }
    
}
extension String{
    
    public func replaceVars(values:String...)->String {
        var str = "\(self)".loc
        if values.count == 1{
            str = str.replacingOccurrences(of: "[XX]", with: "\(values[0])")
//            print("====OOIIIII-----s--s",str)
        }
        if values.count == 2{
            str = str.replacingOccurrences(of: "[XX]", with: "\(values[0])")
            str = str.replacingOccurrences(of: "[YY]", with: "\(values[1])")
        }
        if values.count == 3{
            str = str.replacingOccurrences(of: "[XX]", with: "\(values[0])")
            str = str.replacingOccurrences(of: "[YY]", with: "\(values[1])")
            str = str.replacingOccurrences(of: "[ZZ]", with: "\(values[2])")
        }
        return str
    }
    
    public var loc:String{
        get{
            
            if let b = MoLocalizable.bundle{
                let v = b.localizedString(forKey: "\(self)", value: nil, table: "loc")
                return v
            }
            
            return NSLocalizedString("\(self)", comment: "").removeMarked
        }
    }
    public var removeMarked:String{
        get{
            if self.hasPrefix("[["){
                return self.replacingFirst(matching: "^\\[\\[[^\\[\\]]+\\]\\]", with: "")
            }
            return self
        }
    }
}
